using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lavka.Core;
using Lavka.Models;

namespace Lavka.Controllers
{   
    public class BrandsController : Controller
    {
        private LavkaDbEntities context = new LavkaDbEntities();

        //
        // GET: /Brands/

        public ViewResult Index()
        {
            return View(context.Brands.Include("Products").ToList());
        }

        //
        // GET: /Brands/Details/5

        public ViewResult Details(int id)
        {
            Brands brands = context.Brands.Single(x => x.BrandId == id);
            return View(brands);
        }

        //
        // GET: /Brands/Create
        [Authorize(Roles = "Administrators")]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Brands/Create

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        [ValidateInput(false)]
        public ActionResult Create(Brands brands, HttpPostedFileBase file)
        {

            if (ModelState.IsValid)
            {
                context.Brands.AddObject(brands);
                context.SaveChanges();
                brands.Photo = FileHelper.UploadPhoto(file, "brands", brands.BrandId);
                context.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(brands);
        }
        
        //
        // GET: /Brands/Edit/5
         [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id)
        {
            Brands brands = context.Brands.Single(x => x.BrandId == id);
            return View(brands);
        }

        //
        // POST: /Brands/Edit/5

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        [ValidateInput(false)]
        public ActionResult Edit(Brands brands, HttpPostedFileBase file)
        {
            brands.Photo = FileHelper.UploadPhoto(file, "brands", brands.BrandId);

            if (ModelState.IsValid)
            {
                context.Brands.Attach(brands);
                context.ObjectStateManager.ChangeObjectState(brands, EntityState.Modified);
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(brands);
        }

        //
        // GET: /Brands/Delete/5
         [Authorize(Roles = "Administrators")]
        public ActionResult Delete(int id)
        {
            Brands brands = context.Brands.Single(x => x.BrandId == id);
            return View(brands);
        }

        //
        // POST: /Brands/Delete/5
        [Authorize(Roles = "Administrators")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Brands brands = context.Brands.Single(x => x.BrandId == id);
            context.Brands.DeleteObject(brands);
            context.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}